package com.neusoft.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.mapper.FoodMapper;
import com.neusoft.po.Food;
import com.neusoft.service.FoodService;
import com.neusoft.util.Lock;

@Service
public class FoodServiceImpl implements FoodService{
	
	@Autowired
	private FoodMapper foodMapper;

	@Override
	public List<Food> listFoodByBusinessId(Integer businessId) {
		return foodMapper.listFoodByBusinessId(businessId);
	}

	@Override
	public Food getFoodById(Integer foodId) {
		// TODO Auto-generated method stub
		return foodMapper.getFoodById(foodId);
	}

	@Override
	public int saveFood(Food food) {
		// TODO Auto-generated method stub
		return foodMapper.saveFood(food);
	}

	//transaction
	@Override
	@Transactional
	public int updateFood(Food food) {
		// TODO Auto-generated method stub
		if(!Lock.FoodInfoLock(food)) {
			return 0;
		}
		int updateCount = foodMapper.updateFood(food);
		if (updateCount == 0) {
			return updateCount;
			// 如果更新操作没有影响任何行，返回更新的行数
		} else {
			// 如果更新操作成功，执行插入操作
			return foodMapper.saveFood(food);
		}
	}

	@Override
	@Transactional
	public int removeFood(Integer foodId) {
		// TODO Auto-generated method stub
		if(!Lock.FoodInfoLock(foodMapper.getFoodById(foodId))) {
			return 0;
		}
		return foodMapper.removeFood(foodId);
	}

	@Override
	public int updateFoodCount(Integer foodCount, Integer foodId) {
		// TODO Auto-generated method stub
		return foodMapper.updateFoodCount(foodCount, foodId);
	}
}
